﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using umbraco.BusinessLogic;
using System.Xml;
using umbraco.presentation.nodeFactory;
using AD_Notifications_Dashboard.Helpers;

namespace AD_Notifications_Dashboard.usercontrols
{
    public partial class DashboardNotifications : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try {
                
                Table1.Rows.Clear();
                XmlDocument doc = new XmlDocument();
                doc.Load(HttpContext.Current.Server.MapPath("../data/AD_Notifications_Dashboard.xml"));

                var nodes = doc.SelectNodes("/data/node");

                umbraco.BusinessLogic.User u = umbraco.helper.GetCurrentUmbracoUser();
                // is there a more compact way to make this?
                TableHeaderRow TableHeader = new TableHeaderRow();
                TableHeaderCell Thc1 = new TableHeaderCell();
                TableHeaderCell Thc2 = new TableHeaderCell();
                TableHeaderCell Thc3 = new TableHeaderCell();
                TableHeaderCell Thc4 = new TableHeaderCell();
                Thc4.ColumnSpan = 2;
                Thc1.Text = "Name";
                Thc2.Text = "Date";
                Thc3.Text = "Edited";
                Thc4.Text = "Actions";
                TableHeader.Cells.Add(Thc1);
                TableHeader.Cells.Add(Thc2);
                TableHeader.Cells.Add(Thc3);
                TableHeader.Cells.Add(Thc4);
                Table1.Rows.Add(TableHeader);


                foreach (XmlNode node in nodes)
                {
                    try
                    {
                    

                    int pageID = int.Parse(node["id"].InnerText);
                    Node myNode = new Node(pageID);
                    DateTime nodeUpdateDate = myNode.UpdateDate;
                    DateTime myDateTime = DateTime.Parse(node["date"].InnerText);
                    // delete obsolete entries
                    if (nodeUpdateDate.CompareTo(myDateTime) > 0)
                    {
                        node.ParentNode.RemoveChild(node);
                    }
                    else
                    {

                        

                            TableRow tr = new TableRow();
                            TableCell tdName = new TableCell();
                            TableCell tdDate = new TableCell();
                            TableCell tdEditBy = new TableCell();
                            TableCell tdPublish = new TableCell();
                            TableCell tdPreview = new TableCell();

                            LinkButton lnkPublish = new LinkButton();

                            tdName.Text = "<a href=\"/umbraco/editContent.aspx?id=" + node["id"].InnerText + "\" title=\"Edit\">" + node["name"].InnerText + "</a>";
                            tdDate.Text = myDateTime.ToString("MMM dd h:mm tt");
                            tdEditBy.Text = node["user"].InnerText;
                            lnkPublish.ToolTip = "Publish";
                            lnkPublish.Text = "<img src=\"/umbraco/images/editor/saveAndPublish.gif\"/>";
                            lnkPublish.CommandArgument = node["id"].InnerText;
                            lnkPublish.Click += new System.EventHandler(this.publishClick);
                            tdPublish.Controls.Add(lnkPublish);
                            tdPreview.Text = "<a href=\"javascript:void(0);\" onclick=\"window.open('../" + node["id"].InnerText + ".aspx?umbVersion=" + node["version"].InnerText + "','umbPreview')\"><img src=\"/umbraco/images/editor/vis.gif\"/></a>";
                            tdPreview.ToolTip = "Preview";

                            tr.Cells.Add(tdName);
                            tr.Cells.Add(tdDate);
                            tr.Cells.Add(tdEditBy);
                            if (u.IsAdmin()){
                                tr.Cells.Add(tdPublish);
                            }
                            tr.Cells.Add(tdPreview);

                            Table1.Rows.Add(tr);

                    }
                    }
                    catch (System.Exception ex)
                    {
                        Log.Add(LogTypes.Error, u.Id, ex.StackTrace);
                    }
                }

                if (nodes.Count == 0)
                {

                    Panel1.Visible = true;
                    Table1.Visible = false;
                }
               

                doc.Save(HttpContext.Current.Server.MapPath("../data/AD_Notifications_Dashboard.xml"));
            
            }
            catch (System.Exception ex)
            {
                Log.Add(LogTypes.Error, 0, ex.StackTrace);
            }

        }


        protected void publishClick(object sender, System.EventArgs e)
        {

            LinkButton publishButton = sender as LinkButton;
            int publishId = Convert.ToInt32(publishButton.CommandArgument);
            umbraco.BusinessLogic.User u = umbraco.helper.GetCurrentUmbracoUser();
            UmbracoHelper.PublishItem(publishId, u.Id.ToString());
            
            Page_Load(sender, e); 

        }

      
    }
}